package com.example.demo.server.dao;

import com.example.demo.server.model.UserInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDao {

    public int register(UserInfo userInfo) {
        Connection connection = null;
        PreparedStatement statement = null;
        int ret = 0;
        try {
            connection = DBUtils.getConnection();
            String sql = "insert into userinfo (username, name, password) values(?, ?, ?)";
            statement = connection.prepareStatement(sql);
            // 设置信息
            statement.setString(1, userInfo.getUsername());
            statement.setString(2, userInfo.getName());
            statement.setString(3, userInfo.getPassword());
            // 存储
            ret = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection, statement, null);
        }
        return ret;
    }

    public UserInfo selectByUsername(String username) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet set = null;
        UserInfo userInfo = new UserInfo();
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from userinfo where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            set = statement.executeQuery();
            if(set.next()) {
                userInfo.setId(set.getInt("id"));
                userInfo.setName(set.getString("name"));
                userInfo.setUsername(set.getString("username"));
                userInfo.setPassword(set.getString("password"));
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection, statement, set);
        }
        return userInfo;
    }

    public UserInfo getUserByUid(int uid) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet set = null;
        UserInfo userInfo = new UserInfo();
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from userinfo where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, uid);
            set = statement.executeQuery();
            if(set.next()) {
                userInfo.setId(set.getInt("id"));
                userInfo.setName(set.getString("name"));
                userInfo.setUsername(set.getString("username"));
                userInfo.setPassword(set.getString("password"));
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection, statement, set);
        }
        return userInfo;
    }
}
